package com.ctshk.sapp.order.train.controller;

import com.ctshk.common.annotations.Authority;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.ValidatorUtil;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.train.dto.QueryQuotaDTO;
import com.ctshk.rpc.order.train.dto.TrainOrderDetailDTO;
import com.ctshk.rpc.order.train.dto.TrainOrderPageDTO;
import com.ctshk.rpc.order.train.dto.TrainOrderPayInfoDTO;
import com.ctshk.rpc.order.train.req.*;
import com.ctshk.rpc.order.train.service.ITrainOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 火车票订单表 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-17
 */
@Api(value="预订-订单中心-订单操作", tags="预订-订单中心-订单操作")
@RestController
@RequestMapping("/train-order")
public class TrainOrderController extends BaseController {

    @DubboReference
    private ITrainOrderService trainOrderService;

//    @Autowired
//    private TrainOrderFactory trainOrderFactory;

    @ApiOperation(value = "火车票-订单中心-填写订单", notes = "火车票-订单中心-填写订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "火车票-订单中心-填写订单", operDesc = "火车票-订单中心-填写订单")
    @PostMapping("/addOrder")
    public Result saveTrainOrder(@Valid @RequestBody TrainPlaceOrderReq trainPlaceOrderReq) {
        TokenUser currentUser = getCurrentUser(getRequest());
        return trainOrderService.addOrder(trainPlaceOrderReq, currentUser);
    }

    @ApiOperation(value = "火车票-订单中心-支付订单查询详情", notes = "火车票-订单中心-支付订单查询详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "火车票-订单中心-支付订单查询详情", operDesc = "火车票-订单中心-支付订单查询详情")
    @GetMapping("/queryPayinfo/{id}")
    public Result<TrainOrderPayInfoDTO> paymentInfoById(@PathVariable("id") Long id) {
        return trainOrderService.paymentInfoById(id);
    }

    @ApiOperation(value = "预订中心火车票-支付页面-下单", notes = "预订中心巴士票-支付页面-下单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "预订中心巴士票-支付页面-下单", operDesc = "预订中心巴士票-支付页面-下单")
    @PostMapping(value = "/pay/{id}")
    public Result payOrder(@PathVariable("id") Long id, @RequestBody List<TrainPayOrderCollectionReq> reqList){
        reqList.forEach(ValidatorUtil::validate);
        TokenUser currentUser = getCurrentUser(getRequest());
        return trainOrderService.payOrder(id,reqList,currentUser);
    }

    @ApiOperation(value = "订单中心火车票--取消订单", notes = "订单中心火车票-取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "订单中心火车票--取消订单", operDesc = "订单中心火车票--取消订单")
    @PostMapping(value = "/cancel/order")
    public Result cancelOrder(@RequestBody TrainCancelOrderReq cancelOrderReq){
        TokenUser currentUser = getCurrentUser(getRequest());
        return trainOrderService.cancelOrder(cancelOrderReq,currentUser);
    }

    @ApiOperation(value="订单中心-火车票订单分页查询列表", notes="订单中心-火车票订单分页查询列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心-火车票订单分页查询列表", operDesc = "订单中心-火车票订单分页查询列表")
    @PostMapping("/list")
    @Authority
    public Result<PageResponse<TrainOrderPageDTO>> queryBusLineList(@RequestBody TrainOrderPageReq trainOrderPageReq) {
        TokenUser currentUser = getCurrentUser(getRequest());
        trainOrderPageReq.setCurrentUserId(currentUser.getId());
        PageResponse<TrainOrderPageDTO> response = trainOrderService.queyList(trainOrderPageReq,currentUser);
        return Result.success(response);
    }

    @ApiOperation(value = "订单中心-查询火车票订单详情", notes = "订单中心-查询火车票订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心-查询火车票订单详情", operDesc = "订单中心-查询火车票订单详情")
    @GetMapping(value = "/queryOrderDetailInfo/{orderId}")
    public Result<TrainOrderDetailDTO> queryOrderDetailInfo(@PathVariable Long orderId){
        return trainOrderService.queryOrderDetailInfo(orderId);
    }



    @ApiOperation(value = "订单中心火车票--修改旅客信息", notes = "订单中心火车票-修改旅客信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心火车票--修改旅客信息", operDesc = "订单中心火车票--修改旅客信息")
    @PostMapping(value = "/updateTourist")
    public Result updateTourist(@RequestBody TrainOrderTouristReq touristReq){
        return trainOrderService.updateBusOrderTourist(touristReq,getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "订单中心火车票-订单操作-确认退款", notes = "订单中心火车票-订单操作-确认退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单中心火车票-订单中心-确认退款", operDesc = "订单中心火车票-订单中心-确认退款")
    @PostMapping("/confirm/refund/{id}/{refundId}")
    public Result confirmRefund(@PathVariable("id") Long id, @PathVariable("refundId") Long refundId) {
        Long userId = getCurrentUser(getRequest()).getId();
        return trainOrderService.confirmRefund(id, refundId, userId);
    }

    /**
     * 退款审批
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "退款审批", notes = "退款审批")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "退款审批", operDesc = "退款审批")
    @PostMapping(value = "refundApproval")
    public Result refundApproval(@RequestBody TrainOrderRefundApprovalReq req) {
        return trainOrderService.refundApproval(req, getCurrentUser(getRequest()));
    }

    @ApiOperation("确认火车订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "确认巴士订单", operDesc = "确认巴士订单")
    @PostMapping("confirmTrainOrder")
    public Result<Void> confirmTrainOrder(@RequestBody @Valid TrainOrderConfirmReq req) {
        return trainOrderService.TrainOrder(req, getCurrentUser());
    }

    /**
     * 通知旅客订单确认成功
     *
     * @param sendMessageToTouristReq
     * @return
     */
    @ApiOperation(value = "通知旅客订单确认成功", notes = "通知旅客订单确认成功")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "通知旅客订单确认成功", operDesc = "通知旅客订单确认成功")
    @PostMapping("/sendMessageToTourist")
    public Result sendMessageToTourist(@RequestBody @Valid TrainOrderSendMessageToTouristReq sendMessageToTouristReq) {
        return trainOrderService.sendMessageToTourist(sendMessageToTouristReq);
    }

    @ApiOperation(value = "查询挂账额度", notes = "查询挂账额度")
    @OperationLogAnnotation(operModul = OperationLogModel.FINANCE, operType = OperationLogType.QUERY, operTitle = "挂账额度-查询额度", operDesc = "挂账额度-查询额度")
    @PostMapping("/query-quota")
    public Result<QueryQuotaDTO> queryQuota(@RequestBody @Valid QueryQuotaReq req) {
        return trainOrderService.queryQuota(req);
    }
}
